package com.video.information.ui.home

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.google.gson.Gson
import com.video.information.R
import com.video.information.data.model.HomePageRecommend
import com.video.information.ui.common.ui.BaseFragment
import com.video.information.ui.home.video.VideoFragment
import kotlinx.android.synthetic.main.fragment_home.*

class HomeFragment : BaseFragment() {
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        return super.onCreateView(inflater.inflate(R.layout.fragment_home, container, false))
    }

    private val fragments = ArrayList<Fragment>()

    private val titles = ArrayList<String>()

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        val data = Gson().fromJson(HomePageRecommend.mTitles, HomePageRecommend::class.java).RECORDS
        data.forEach {
            titles.add(it.category_name)
            fragments.add(VideoFragment.newInstance(it.category_id))
        }
        fixedViewPager.offscreenPageLimit = fragments.size
        fixedViewPager.adapter = HomeVideoTypeAdapter(requireFragmentManager(), titles, fragments)
        slidingTabLayout.setViewPager(fixedViewPager)
    }

    companion object {
        fun newInstance() = HomeFragment()
    }
}